package com.gxa.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.CarriersContractMapper;
import com.gxa.modules.pojo.CarriersContract;
import com.gxa.modules.service.CarriersContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class CarriersContractServiceImpl extends ServiceImpl<CarriersContractMapper, CarriersContract> implements CarriersContractService {

    @Autowired
    private CarriersContractMapper carriersContractMapper;

    @Override
    public JsonResult findCarriersContractList(Long currentPage, Long pageSize) {
        JsonResult jr=new JsonResult();
        try{
            Long start=(currentPage-1)*pageSize;
            Long off=pageSize;
            Page<CarriersContract> page=new Page<>(start,off);
            Page<CarriersContract> carriersContractPage=carriersContractMapper.selectPage(page,null);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount((int) carriersContractPage.getTotal());
            jr.setData(carriersContractPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络开小差");
        }
        return jr;
    }

    @Override
    public JsonResult findCarriersContractByCondition(Long currentPage, Long pageSize, String name, String contract_name, String add_time) {
        JsonResult jr=new JsonResult();
        try{
            Long start=(currentPage-1)*pageSize;
            Long off=pageSize;
            Page<CarriersContract> page=new Page<>(start,off);
            QueryWrapper<CarriersContract> queryWrapper=new QueryWrapper();
            queryWrapper.eq(StringUtils.isNotBlank(name),"name",name)
                        .eq(StringUtils.isNotBlank(contract_name),"contract_name",contract_name)
                        .eq(StringUtils.isNotBlank(add_time),"add_time",add_time);
            Page<CarriersContract> carriersContractPage=carriersContractMapper.selectPage(page,queryWrapper);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount((int) carriersContractPage.getTotal());
            jr.setData(carriersContractPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络开查小差");
        }
        return jr;
    }

    @Override
    public R queryCarriersContractById(Integer id) {
        R r=new R();
        try{
            CarriersContract carriersContract = carriersContractMapper.selectById(id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(carriersContract);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }

    @Override
    public R delCarriersContractById(Integer id) {
        R r=new R();
        try{
            int result = carriersContractMapper.deleteById(id);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }

    @Override
    public R addCarriersContract(CarriersContract carriersContract) {
        R r=new R();
        try{
            Date date = new Date();
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
            String datetime = format.format(date);
            carriersContract.setContractTime(datetime);
            carriersContract.setAddTime(datetime);
            int result = carriersContractMapper.insert(carriersContract);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }
}
